import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by fjc on 2017/3/14.
 */
public class Set {
    //直接创建jedis
//    Jedis jedis = new Jedis("localhost",6379);
    //创建对象池
    JedisPool jedisPool = new JedisPool(new JedisPoolConfig(),"localhost",6379);
    //获取jedis
    Jedis jedis = jedisPool.getResource();

    @Test
    public void sAdd(){
        //向集合添加一个或多个成员
        jedis.sadd("set1","1","2");
    }

    @Test
    public void sCard(){
        //获取集合的成员数
        System.out.println(jedis.scard("set1"));
    }

    @Test
    public void sDiff(){
        //返回给定所有集合的差集
        jedis.sadd("set2","1","2","3");
        jedis.sadd("set3","3","4","5");
        System.out.println(jedis.sdiff("set2","set3"));
    }

    @Test
    public void sDiffStore(){
        //返回给定所有集合的差集并存储在指定集合
        jedis.sadd("set2","1","2","3");
        jedis.sadd("set3","3","4","5");
        jedis.sdiffstore("set4","set2","set3");
        System.out.println(jedis.scard("set4"));
    }

    @Test
    public void sIsMenber(){
        //判断 member 元素是否是集合 key 的成员
        System.out.println(jedis.sismember("set3","3"));
    }

    @Test
    public void sMembers(){
        //返回集合中的所有成员
        System.out.println(jedis.smembers("set3"));
    }

    @Test
    public void sMove(){
        //将 member 元素从 集合移动到 另一个 集合
        jedis.sadd("set2","1","2","3");
        jedis.sadd("set3","3","4","5");
        jedis.smove("set2","set3","1");
        System.out.println(jedis.smembers("set2"));
        System.out.println(jedis.smembers("set3"));
    }

    @Test
    public void sPop(){
        //移除并返回集合中的一个随机元素
        jedis.sadd("set2","1","2","3");
        System.out.println(jedis.spop("set2"));
        System.out.println(jedis.smembers("set2"));
    }

    @Test
    public void sRandMember(){
        //返回集合中一个或多个随机数
        jedis.sadd("set2","1","2","3");
        System.out.println(jedis.srandmember("set2"));
        //可设置返回的个数
        System.out.println(jedis.srandmember("set2",2));
    }

    @Test
    public void sRem(){
        //移除集合中一个或多个成员
        jedis.sadd("set2","1","2","3");
        jedis.srem("set2","1","2");
        System.out.println(jedis.smembers("set2"));
    }

    @Test
    public void sUnion(){
        //返回所有给定集合的并集
        jedis.sadd("set2","1","2","3");
        jedis.sadd("set3","3","4","5");
        System.out.println(jedis.sunion("set2","set3"));
    }

    @Test
    public void sUnionStore(){
        //返回给定所有集合的并集并存储在指定集合
        jedis.sadd("set2","1","2","3");
        jedis.sadd("set3","3","4","5");
        jedis.sunionstore("set4","set2","set3");
        System.out.println(jedis.scard("set4"));
    }

}
